﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
namespace PasteLoggerBoard.usermodels
{
    /// <summary>
    /// 推送对象
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "root", "root" })]
    public class WebHookerAppService : PasteLoggerBoardAppService
    {

        private readonly IPasteLoggerBoardDbContext _dbContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        public WebHookerAppService(IPasteLoggerBoardDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 按页获取推送对象
        ///</summary>
        /// <param name="page">页码</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<WebHookerListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.WebHooker.Where(t => 1 == 1).OrderByDescending(xy => xy.Id);
            var _pagedto = new PagedResultDto<WebHookerListDto>();
            _pagedto.TotalCount = await query.CountAsync();
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<WebHooker>, List<WebHookerListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 根据ID获取单项推送对象
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookerDto GetByIdAsync(int id)
        {
            var query = _dbContext.WebHooker.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHooker, WebHookerDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息推送对象
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookerUpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.WebHooker.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHooker, WebHookerUpdateDto>(query);
            return temList;
        }

        /// <summary>
        /// 添加一个推送对象
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookerDto> CreateItemAsync(WebHookerAddDto input)
        {

            var newu = ObjectMapper.Map<WebHookerAddDto, WebHooker>(input);
            newu.IsEnable = true;            //添加自定义
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<WebHooker, WebHookerDto>(newu);
            return backinfo;
        }

        /// <summary>
        /// 更新一个推送对象
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookerDto> UpdateItemAsync(WebHookerUpdateDto input)
        {
            var info = await _dbContext.WebHooker.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new UserFriendlyException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<WebHookerUpdateDto, WebHooker>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<WebHooker, WebHookerDto>(info);
            return backinfo;
        }
    }
}
